var adList = ["images/ad1.jpg","images/ad2.jpg","images/ad1.jpg","images/ad2.jpg"];
var curAdId = 0;
var epgTimer = null;
function showEpgBox(data){ 
	if(!data)return ;
	if(epgTimer){clearTimeout(epgTimer);epgTimer = null;}
	var img = $.getImageUrl('../'+data.icon,'../images/tv_default.png');
	var iconBox = document.getElementById('iconBox');
	iconBox.innerHTML = '<img src="'+img+'"/>';
	
	var epgBoxMidTitle = document.getElementById('epgBoxMidTitle');
	epgBoxMidTitle.innerHTML = '.&nbsp;'+data.name;
	
	var epgBoxList = document.getElementById('epgBoxList');
	var epgContent = '';
	var programList = false;  
	if(programList){
		try{
			eval("var propMap = "+programList); 
			var pList = propMap.propMap.program; 
			var con = '';
			for(var i = 0;i<pList.length;i++){ if(i>=3)break;
				var program = pList[i];
				con += '<div class="epgItem">';
					con += '<div class="epgDot"><img src="images/tvepg_dots.png"/></div>';
					con += '<div class="epgTime">'+formatEpgItem(program.beginTime)+'-'+formatEpgItem(program.endTime)+'</div>';
					con += '<div class="epgName">'+program.name+'</div>';
				con += '</div>';
			}
			epgContent = con;
		}catch(e){
			alert(e);
		}
	}
	if(!epgContent){
		epgContent += '<div class="epgItem">&nbsp;</div>';
		epgContent += '<div class="epgItem">';
			epgContent += '<div class="epgDot"><img src="images/tvepg_dots.png"/></div>';
			epgContent += '<div class="epgName">&nbsp;</div>';
		epgContent += '</div>';
	}
	epgBoxList.innerHTML = epgContent;
	
	var epgAdBox = document.getElementById('epgAdBox');
	var ad = '';
	/*
	if(data.propMap && data.propMap.ads && data.propMap.ads.length > 0){
		var id = getRandomId(data.propMap.ads.length);
		ad = '<img src="'+$.getImageUrl(data.propMap.ads[id -1].imageUrl,'../images/icon.png')+'"/>';
	}
	*/
	//var id = getRandomId(adList.length);
	//ad = '<img src="'+adList[id - 1]+'"/>';
	//epgAdBox.innerHTML = ad;
	var epgBox = document.getElementById('epgBox');
	epgBox.style.display = 'block';

}

function hideEpgBox(){
	epgTimer = setTimeout("hideEpgBoxAuto()",3000);
}

function hideEpgBoxAuto(){
	
	var epgBox = document.getElementById('epgBox');
	epgBox.style.display = 'none';
	
}


function formatEpgItem(time){
	var reCat = /\d{1,2}:\d{1,2}/i;
	var t = reCat.exec(time);
	return t;
	
}

function getRandomId(n){
	return parseInt(Math.random()*(n)+1);
}

var errorTipTimer = null;
function showErrorTip(data){
	if(errorTipTimer) clearTimeout(errorTipTimer);
	
	showEpgBox(data);
	var epgBoxTip = document.getElementById('epgBoxTip');
	epgBoxTip.innerHTML= lang.live.errorPlay;
	
	errorTipTimer = setTimeout("hideErrorTip()",4000);
}
function hideErrorTip(){
	if(errorTipTimer) clearTimeout(errorTipTimer);
	var epgBoxTip = document.getElementById('epgBoxTip');
	epgBoxTip.innerHTML= '&nbsp;';
	hideEpgBoxAuto();
}

function changeEPGBuffer(per){
	$('#epgBoxTip').html(per+'%').show();
}
function hideEPGBuffer(){
	$('#epgBoxTip').hide();
	
}


function showOnBuffer(data){
	var epgBoxTip = document.getElementById('epgBoxTip');
	var dis = epgBoxTip.style.display;
	if(dis!='block'){
		showEpgBox(data);
	}
	var percent = player.getPlayerBuf() * 100 /(player.getPlayBufferTime () );
	percent = parseInt(percent);
	var epgBoxTip = document.getElementById('epgBoxTip');	
	if(percent >= 100){
		hideOnBuffer();
	}else{
		epgBoxTip.innerHTML = percent+'%';
	}
}

function hideOnBuffer(){
	hideEpgBoxAuto();
}